package com.mjh.partners.controller;

import com.mjh.partners.pojo.Experience;
import com.mjh.partners.pojo.Praise;
import com.mjh.partners.service.CommentService;
import com.mjh.partners.service.ExperienceService;
import com.mjh.partners.service.PraiseService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RequestMapping("/ExperiecneCotroller")
@Controller
public class ExperiecneCotroller {
    @Autowired
    private ExperienceService experienceService;
    @Autowired(required = false)
    private PraiseService praiseService;
    @Autowired
    private CommentService commentService;
    @RequestMapping(value="getAllExperience",produces = {"application/json;charset=UTF-8"})
    public String getAllExperience(Model model){
        List<Experience> Lists=experienceService.getAllExperience();
        model.addAttribute("ListEx",Lists);
        return "index";
    }
    @ResponseBody
    @RequestMapping(value="getExperienceByEid",produces = {"application/json;charset=UTF-8"})
    public String getExperienceByEid(int eid, int userid, HttpServletRequest request, HttpServletResponse response, Model model) throws  Exception{
        System.out.println(eid);
        System.out.println(userid);
        Experience Lists=experienceService.getExperienceByEid(eid);
        boolean praise_flag;
        Praise praise=null;
        if(userid==0) {
            praise=null;
        }else {
            praise=praiseService.getPraise(eid, userid);
        }

        if(praise!=null) {
            praise_flag=true;
        }else {
            praise_flag=false;
        }
        Lists.setPraise_flag(praise_flag);
        response.setContentType("text/html;charset=utf-8");
        JSONArray json = JSONArray.fromObject(Lists);
        String result=json.toString();
        return result;
    }
    public String upload(String base64Ex, Date dt, int userid) {
        String ExImage = null;
        BASE64Decoder decoder = new BASE64Decoder();
        try   {
            byte[] b = decoder.decodeBuffer(base64Ex);
            for(int i=0;i<b.length;++i)
            {
                if(b[i]<0)
                {
                    b[i]+=256;
                }
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String fileName = "Eximg_"+sdf.format(dt)+String.valueOf(userid)+".png";
            String imgFilePath = "C:\\picture132\\experience\\"+fileName;
            ExImage= "http://localhost:8080/Partners/picture132/experience/"+fileName;
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return ExImage;
        }catch (Exception e) {
            // TODO: handle exception
            return "error";
        }

    }
    @ResponseBody
    @RequestMapping("addExperience")
    public String addExperience(int userid,String base64Ex,String eTitle,String eContent){
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String ExImage=null;;
        Experience experience=new Experience();
        String nowString=sdf.format(dt);
        experience.seteTitle(eTitle);
        experience.seteContent(eContent);
        if(base64Ex.length()>5) {
            ExImage=upload(base64Ex,dt,userid);//这是个base64转换成图片保存在本地，并返回地址的方法，篇幅问题不予展示
            experience.seteImage(ExImage);
        }
        experience.setUserid(userid);
        experience.setCreatetime(nowString);;
        boolean flag=experienceService.addExperience(experience);
        if(flag) {
            return "success";
        }else {
            return "erorr";
        }
    }
    @ResponseBody
    @RequestMapping("deleteEx")
    public String deleteEx(int eid) {
        boolean flag1=commentService.deleteByEx(eid);
        boolean flag2=praiseService.deleteByEx(eid);
        boolean flag3=experienceService.deleteEx(eid);
        if(flag1&&flag2&&flag3) {
            return "success";
        }else {
            return "erorr";
        }
    }
    @ResponseBody
    @RequestMapping(value="getExperienceByUserid",produces = {"application/json;charset=UTF-8"})
    public String getExperienceByUserid(int userid,HttpServletRequest request,HttpServletResponse response) throws  Exception{
        List<Experience> Lists=experienceService.getExperienceByUserid(userid);
        response.setContentType("text/html;charset=utf-8");
        JSONArray json = JSONArray.fromObject(Lists);
        String result=json.toString();
        return result;
    }
}

